/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef NODE_UTIL_XMLFILE_H_
#define NODE_UTIL_XMLFILE_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <string>
#include <tinyxml.h>
#include "node/macros.h"

namespace node {
    namespace util {

        class XmlFile
        {
        public:
            typedef TiXmlElement    Node;
            typedef TiXmlText       Text;

            XmlFile(void);
            ~XmlFile(void);

            bool LoadFile(const char* filename,
                TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING) {
                return doc_.LoadFile(filename, encoding);
            }

            static XmlFile* Create(const char* filename,
                const char* version = "1.0",
                const char* encoding = "utf8",
                const char* standalone = "no");
            static XmlFile* Open(const char* filename,
                TiXmlEncoding encoding = TIXML_DEFAULT_ENCODING);

            inline bool error(void) const {
                return doc_.Error();
            }
            inline const char* error_string(void) const {
                return doc_.ErrorDesc();
            }

            inline bool Save(void) {
                return doc_.SaveFile(filename_.c_str());
            }
            inline bool Save(const char* filename) {
                return doc_.SaveFile(filename);
            }

            Node* CreateRootNode(const char* name);
            inline Node* CreateNode(const char* name) {
                return new Node(name);
            }
            inline Text* CreateText(const char* name) {
                return new Text(name);
            }

            inline Node* GetRootNode(void) {
                return doc_.RootElement();
            }
            inline const Node* GetRootNode(void) const {
                return doc_.RootElement();
            }

            Node* Find(const char* path);
            Node* FindOrCreate(const char* path);

            const char* GetText(const char* path, const char* default = "");
            int GetInt(const char* path, int default = 0);
            long GetLong(const char* path, long default = 0);
            double GetDouble(const char* path, double default = 0.0f);

            bool SetText(const char* path, const char* value);
            bool SetInt(const char* path, int value);
            bool SetLong(const char* path, long value);
            bool SetDouble(const char* path, double value);

        private:
            mutable TiXmlDocument doc_;
            std::string filename_;

            DISALLOW_COPY_AND_ASSIGN(XmlFile);
        };

    }
}

#endif // NODE_UTIL_XMLFILE_H_
